<?php
/**
 * @Author: Ron Chen
 * @Date:   2017-03-25 22:05:56
 * @Last Modified by:   Ron Chen
 * @Last Modified time: 2017-06-11 21:50:22
 */

namespace app\models;

use Yii;

/**
 * 投保人 -- 一个保单=> 对应一个投保人
 */
class InsurancePolicyHolder extends \yii\db\ActiveRecord
{
    const STATUS_POLICY_SUCCESS = 'ok';
    const STATUS_POLICY_FAILD   = 'false';

    const ID_CARD_TYPE_ZERO = 0;
    const ID_CARD_TYPE_ONE  = 1;
    const ID_CARD_TYPE_TWO  = 2;
    const ID_CARD_TYPE_THR  = 3;
    const ID_CARD_TYPE_FOUR = 4;
    const ID_CARD_TYPE_FIVE = 5;

    public static $userIdCard = [
        self::ID_CARD_TYPE_ZERO => "未知",
        self::ID_CARD_TYPE_ONE  => "身份证",
        self::ID_CARD_TYPE_TWO  => "出生证",
        self::ID_CARD_TYPE_THR  => "护照",
        self::ID_CARD_TYPE_FOUR => "其他",
    ];

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'insurance_policy_holder';
    }
    
    /**
     * 初始化用户操作
     */
    public static function initRecord($param){
    	$policy_id = isset($param["policy_id"]) ? $param["policy_id"] : 1;
    	
        $userInfo = static::find()->where(['policy_id'=>$policy_id])->limit(1)->one();
        if (empty($userInfo)) {
            $userInfo = new static();
            $userInfo->policy_id   = $policy_id;
            $userInfo->create_time = date("Y-m-d H:i:s");
            $userInfo->status      = self::STATUS_POLICY_SUCCESS;
        }
		
        if (isset($param['policy_holder_type']) && $param['policy_holder_type']) {
            $userInfo->policy_holder_type = $param['policy_holder_type'];
        }
    	if (isset($param['policy_holder_name']) && $param['policy_holder_name']) {
    		$userInfo->policy_holder_name = $param['policy_holder_name'];
    	}
    	if (isset($param['id_type']) && $param['id_type']) {
    		$userInfo->id_type = $param['id_type'];
    	}
    	if (isset($param['id_number']) && $param['id_number']) {
    		$userInfo->id_number = $param['id_number'];
    	}
    	if (isset($param['birthday']) && $param['birthday']) {
    		$userInfo->birthday = $param['birthday'];
    	}
    	if (isset($param['telephone']) && $param['telephone']) {
    		$userInfo->telephone = $param['telephone'];
    	}
        if (isset($param['address']) && $param['address']) {
            $userInfo->address = $param['address'];
        }
        if (isset($param['postcode']) && $param['postcode']) {
            $userInfo->postcode = $param['postcode'];
        }
    	if (isset($param['email']) && $param['email']) {
    		$userInfo->email = $param['email'];
    	}

    	if ($userInfo->save()) {
    		return $userInfo->attributes['policy_id'];
    	}
    	return false;
    }	
}